<?php

$curl_errno = 0;
require_once dirname(__FILE__) . '/autoload.php';
$timeStart = time();
require_once dirname(__FILE__) . '/vpslist.config.php';

$PbWarningVPS = array(
    '172.245.5.169',
    '172.245.5.150',
    '192.210.195.104',
	'198.23.241.5',
	'103.27.126.125'
);

$monitorIps = array(
);

foreach ($arrVpsData as $IpAddress => $vpsData) {
    $monitorIps[] = 'm' . str_replace('.', '-', $IpAddress) . '.u631.com';
}

$vpsCount = count($arrVpsData);
$warningCount = 0;


$HTMLBODY = '';
$HTMLBODY.= '<b>[' . $vpsCount . '个]服务器状态报告(' . date('Y-m-d H:i:s') . ')</b>
          <table border="1" width="100%" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 14px;">';
$HTMLBODY.='<tr height="35">
    <td width="100" bgcolor="#CCCCFF"><b>IP&nbsp;</b></td>
    <td width="100" bgcolor="#CCCCFF"><b>Mysqld&nbsp;</b></td>
    <td width="100" bgcolor="#CCCCFF"><b>Httpd</b></td>
    <td width="100" bgcolor="#CCCCFF"><b>网站数量</b></td>
	<td width="100" bgcolor="#CCCCFF"><b>数据库连接</b></td>
	<td width="100" bgcolor="#CCCCFF"><b>查询详情</b></td>
	</tr>
    ';

$isWarning = false;

foreach ($monitorIps as $Ip) {
    $ISWCOUNTJIA = false;
    $url = 'http://' . $Ip . '/server_api.php';
    $content = curl_get($url);
    !$content ? $content = curl_get($url) : true;
    if (!$content) {
        $isWarning = true;
        $HTMLBODY.='<tr height="35">
    <td colspan="6"><pre><font color="red">读取' . $Ip . '失败[' . $curl_errno . ']</font>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="' . $url . '?isprint=1" target="_blank">查看API</a></pre></pre></td>
	</tr>
    ';
        $ISWCOUNTJIA = true;
        $warningCount++;
        continue;
    }
    $arrResult = json_decode($content, true);
    if (!is_array($arrResult) || empty($arrResult)) {
        $ISWCOUNTJIA = true;
        $warningCount++;
        $isWarning = true;
        $HTMLBODY.='<tr height="35">
    <td colspan="6"><pre><font color="red">读取' . $Ip . '成功[格式解析失败]</font>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="' . $url . '?isprint=1" target="_blank">查看API</a></pre></td>
	</tr>
    ';
        continue;
    }

    if ($arrResult['mysqld_count'] >= 3) {
        $mysqld = '<font color="green">OK</font>';
    } else {
        $mysqld = '<font color="red">WARNING</font>';

        if (!in_array($arrResult['ip'], $PbWarningVPS)) {
            $isWarning = true;
            $ISWCOUNTJIA = true;
        }
    }

    if ($arrResult['httpd_count'] >= 3) {
        $httpd = '<font color="green">OK</font>';
    } else {
        $httpd = '<font color="red">WARNING</font>';
        if (!in_array($arrResult['ip'], $PbWarningVPS)) {
            $isWarning = true;
            $ISWCOUNTJIA = true;
        }
    }

    $HTMLBODY.='<tr height="35">
    <td>' . $arrResult['ip'] . '</td>
    <td>' . $arrResult['mysqld_count'] . '[' . $mysqld . ']</td>
    <td>' . $arrResult['httpd_count'] . '[' . $httpd . ']</td>
    <td>' . $arrResult['site_count'] . '</td>
	 <td>' . $arrResult['mysqlconn'] . '</td>
	<td><a href="' . $url . '?isprint=1" target="_blank">查看API</a></td>
	</tr>
    ';


    $HTMLBODY1.='<tr height="35">
    <td colspan="6" bgcolor="#B4CDCD"><pre>' . $arrResult['ip'] . '</pre></td>
	</tr>
    ';

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>uptime:' . $arrResult['uptime'] . '</pre></td>
	</tr>
    ';

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>df-h:' . $arrResult['df-h'] . '</pre></td>
	</tr>
    ';

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>logsize:' . $arrResult['logsize'] . '</pre></td>
	</tr>
    ';

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>mysqlsize:' . $arrResult['mysqlsize'] . '</pre></td>
	</tr>
    ';

    $HTMLBODY1.='<tr height="35">
    <td colspan="6"><pre>free -m:' . $arrResult['free-m'] . '</pre></td>
	</tr>
    ';

    if ($ISWCOUNTJIA) {
        $warningCount++;
    }
}

$HTMLBODY.=$HTMLBODY1 . '</table>';


if ($isWarning) {
    $arrUsers = array(
        'think889977@sina.com',
    );
    $timeEnd = time();
    $HTMLBODY.='<hr />总计耗时:' . ($timeEnd - $timeStart) . '秒<hr />';
    post_email_api('[严重警告(' . $warningCount . '个)]服务器(总计:' . $vpsCount . '个)异常(' . $warningCount . '个)[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);
} else {
    //发送正常邮件报告
    $time = time();
    $HTMLBODY.='</table>';
    $arrUsers = array(
        'think889977@sina.com',
    );
    $timeEnd = time();
    $HTMLBODY.='<hr />总计耗时:' . ($timeEnd - $timeStart) . '秒<hr />';
    post_email_api('[服务器正常(' . $vpsCount . '个)]状态报告(总计:' . $vpsCount . '个)[' . date('Y-m-d H:i:s') . ']', $HTMLBODY, $arrUsers);
}

function curl_get($post_url) {
    return curl_post1($post_url, array(), 0);
}

function curl_post1($post_url, $data, $ispost = 1) {
    global $curl_errno;
    $post = http_build_query($data);
    $login = curl_init($post_url); //创建CURL对象  
    curl_setopt($login, CURLOPT_HEADER, 0); //返回头部  
    curl_setopt($login, CURLOPT_RETURNTRANSFER, 1); //返回信息  
    curl_setopt($login, CURLOPT_POST, $ispost); //设置POST提交  
    curl_setopt($login, CURLOPT_POSTFIELDS, $post); //提交POST数据  
    curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($login, CURLOPT_TIMEOUT, 30);

    $data = curl_exec($login); //执行已经定义的设置  
    if ($curl_errno = curl_errno($login)) {
        return false;
    }
    curl_close($login); //关闭 */ 
    return $data;
}

?>